
<html>
<head>
    <meta charset="UTF-8">
    <title>分页显示成语</title>
    <style>
        h1, b{ color:red;}
        a{
            color: gray;
            text-decoration: none;
        }
        a:hover{
            color: red;
            text-decoration: underline;
        }
        .list{
            display: flex;
            /*width: 400px;*/
            justify-content: space-around;
            border: 5px double red;
            padding: 10px;
        }
        .list>div{
            border: 1px solid gray;
            padding: 10px;
            min-width: 65px;
            word-break: keep-all;
        }
    </style>
</head>
<body><?php
$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
$count=$db->query("select count(*) from chengyu")->fetchColumn();
$lanNums=11;
$lanSize=25;
$pageSize=$lanNums*$lanSize;
$pageTotal=ceil($count/$pageSize);
$page=(int)$_GET['page'];
if($page<1)$page=1;
else if($page>$pageTotal)$page=$pageTotal;
$n=25;
$begin=$page-$n;
$over=$page+$n;
if($begin<1){
    $begin=1;
    $over=2*$n+1;
}else if($over>$pageTotal){
    $begin=$pageTotal-2*$n;
    $over=$pageTotal;
}
if($begin<1)$begin=1;
if($over>$pageTotal)$over=$pageTotal;
?>
<h1>中国成语大全</h1>
<div class="nav"><?php
    for($i=$begin;$i<=$over;$i++){
        if($i==$page) echo "\t<b>$i</b>\n";
        else echo "\t<a href='?page=$i'>$i</a>\n";
    }
    ?>
</div>
<div class="list"><?php
    $ps=$db->prepare("select cy from chengyu limit ?,?");
    $ps->bindValue(1, ($page-1)*$pageSize, PDO::PARAM_INT);
    $ps->bindValue(2, $pageSize, PDO::PARAM_INT);
    $ps->execute();

    for($i=0;$i<$lanNums;++$i){
        echo "\t<div>\n";
        for($j=0;$j<$lanSize;++$j){
            echo "\t\t<div>{$ps->fetchColumn()}</div>\n";
        }
        echo "</div>";
    }
    ?>
</div>
</body>
</html>
